package top.jiachunlei.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/9 20:14
 */
public class Composite extends Component
{
    private List<Component> components = new ArrayList<>();

    public Composite(String name)
    {
        super(name);
    }

    @Override
    public void add(Component c)
    {
        components.add(c);
    }

    @Override
    public void remove(Component c)
    {
        components.remove(c);
    }

    @Override
    public void display(int depth)
    {
        StringBuffer sb = new StringBuffer("-");
        for (int i = 0; i <= depth; i++)
        {
            sb.append("-");
        }
        System.out.println(sb.toString() + name);
        for (Component component : components)
        {
            component.display(depth+2);
        }
    }
}
